<%@ page contentType="text/html;charset=UTF-8" %>
<script>
$(document).ready(function() {
	$('#studentTable').bootstrapTable({

		  //请求方法
               method: 'post',
               //类型json
               dataType: "json",
               contentType: "application/x-www-form-urlencoded",
               //显示检索按钮
		      showSearch: true,
               //显示刷新按钮
               showRefresh: true,
               //显示切换手机试图按钮
               showToggle: true,
               //显示 内容列下拉框
    	       showColumns: true,
    	       //显示到处按钮
    	       showExport: true,
    	       //显示切换分页按钮
    	       showPaginationSwitch: true,
    	       //显示详情按钮
    	       detailView: false,
    	       	//显示详细内容函数
	           detailFormatter: "detailFormatter",
    	       //最低显示2行
    	       minimumCountColumns: 2,
               //是否显示行间隔色
               striped: true,
               //是否使用缓存，默认为true，所以一般情况下需要设置一下这个属性（*）
               cache: false,
               //是否显示分页（*）
               pagination: true,
                //排序方式
               sortOrder: "asc",
               //初始化加载第一页，默认第一页
               pageNumber:1,
               //每页的记录行数（*）
               pageSize: 10,
               //可供选择的每页的行数（*）
               pageList: [10, 25, 50, 100],
               //这个接口需要处理bootstrap table传递的固定参数,并返回特定格式的json数据
               url: "${ctx}/buzz/student/student/data",
               //默认值为 'limit',传给服务端的参数为：limit, offset, search, sort, order Else
               //queryParamsType:'',
               ////查询参数,每次调用是会带上这个参数，可自定义
               queryParams : function(params) {
               	var searchParam = $("#searchForm").serializeJSON();
               	searchParam.pageNo = params.limit === undefined? "1" :params.offset/params.limit+1;
               	searchParam.pageSize = params.limit === undefined? -1 : params.limit;
               	searchParam.orderBy = params.sort === undefined? "" : params.sort+ " "+  params.order;
                   return searchParam;
               },
               //分页方式：client客户端分页，server服务端分页（*）
               sidePagination: "server",
               contextMenuTrigger:"right",//pc端 按右键弹出菜单
               contextMenuTriggerMobile:"press",//手机端 弹出菜单，click：单击， press：长按。
               contextMenu: '#context-menu',
               onContextMenuItem: function(row, $el){
                   if($el.data("item") == "edit"){
                   		edit(row.id);
                   }else if($el.data("item") == "view"){
                       view(row.id);
                   } else if($el.data("item") == "delete"){
                        jp.confirm('确认要删除该学生登记	记录吗？', function(){
                       	jp.loading();
                       	jp.get("${ctx}/buzz/student/student/delete?id="+row.id, function(data){
                   	  		if(data.success){
                   	  			$('#studentTable').bootstrapTable('refresh');
                   	  			jp.success(data.msg);
                   	  		}else{
                   	  			jp.error(data.msg);
                   	  		}
                   	  	})

                   	});

                   }
               },

               onClickRow: function(row, $el){
               },
               	onShowSearch: function () {
			$("#search-collapse").slideToggle();
		},
               columns: [{
				   field: 'number',
				   title: '序号',
				   width:5 ,
				   align:'center',
				   switchable:false,
				   formatter:function(value,row,index){
					   //return index+1; //序号正序排序从1开始
					   var pageSize=$('#studentTable').bootstrapTable('getOptions').pageSize;//通过表的#id 可以得到每页多少条
					   var pageNumber=$('#studentTable').bootstrapTable('getOptions').pageNumber;//通过表的#id 可以得到当前第几页
					   if (pageSize == "All"){
						   return index  +1;
					   }else {
						   return pageSize * (pageNumber - 1) + index + 1;    //返回每条的序号： 每页条数 * （当前页 - 1 ）+ 序号
					   }
				   }
			   },{
				   checkbox: true

			   }
			,{
					   field: 'name',
					   title: '学生姓名',
					   sortable: true,
					   sortName: 'name'
		        ,formatter:function(value, row , index){
		        	value = jp.unescapeHTML(value);
				   <c:choose>
					   <c:when test="${fns:hasPermission('buzz:student:student:edit')}">
					      return "<a href='javascript:edit(\""+row.id+"\")'>"+value+"</a>";
				      </c:when>
					  <c:when test="${fns:hasPermission('buzz:student:student:view')}">
					      return "<a href='javascript:view(\""+row.id+"\")'>"+value+"</a>";
				      </c:when>
					  <c:otherwise>
					      return value;
				      </c:otherwise>
				   </c:choose>
		         }

		    }
			,{
		        field: 'sex',
		        title: '性别',
		        sortable: true,
		        sortName: 'sex',
					   formatter:function(value, row , index){
						   return jp.getDictLabel(${fns:toJson(fns:getDictList('Sys_XingBie'))}, value, "-");
					   }

		    }
			,{
		        field: 'schoo.name',
		        title: '就读学校',
		        sortable: true,
		        sortName: 'schoo.name'

		    }
			,{
		        field: 'grade.name',
		        title: '就读年级',
		        sortable: true,
		        sortName: 'grade.name'

		    }
			,{
		        field: 'banji.name',
		        title: '就读班级',
		        sortable: true,
		        sortName: 'banji.name'

		    }
			,{
		        field: 'bangan.name',
		        title: '班干',
		        sortable: true,
		        sortName: 'bangan.name'

		    }
			,{
		        field: 'card',
		        title: '证件号',
		        sortable: true,
		        sortName: 'card'

		    }
			,{
		        field: 'xuehao',
		        title: '学号',
		        sortable: true,
		        sortName: 'xuehao'

		    }
			,{
				field: 'remarks',
				title: '备注信息',
				sortable: true,
				sortName: 'remarks'

		    }
		     ]

		});


	  if(navigator.userAgent.match(/(iPhone|iPod|Android|ios)/i)){//如果是移动端


		  $('#studentTable').bootstrapTable("toggleView");
		}

	  $('#studentTable').on('check.bs.table uncheck.bs.table load-success.bs.table ' +
                'check-all.bs.table uncheck-all.bs.table', function () {
            $('#remove').prop('disabled', ! $('#studentTable').bootstrapTable('getSelections').length);
            $('#view,#edit').prop('disabled', $('#studentTable').bootstrapTable('getSelections').length!=1);
        });

		$("#btnImport").click(function(){
			jp.open({
			    type: 2,
                area: [500, 200],
                auto: true,
			    title:"导入数据",
			    content: "${ctx}/tag/importExcel" ,
			    btn: ['下载模板','确定', '关闭'],
				    btn1: function(index, layero){
					  jp.downloadFile('${ctx}/buzz/student/student/import/template');
				  },
			    btn2: function(index, layero){
				        var iframeWin = layero.find('iframe')[0]; //得到iframe页的窗口对象，执行iframe页的方法：iframeWin.method();
						iframeWin.contentWindow.importExcel('${ctx}/buzz/student/student/import', function (data) {
							if(data.success){
								jp.success(data.msg);
								refresh();
							}else{
								jp.error(data.msg);
							}
						   jp.close(index);
						});//调用保存事件
						return false;
				  },

				  btn3: function(index){
					  jp.close(index);
	    	       }
			});
		});
	  $("#export").click(function(){//导出Excel文件
	        var searchParam = $("#searchForm").serializeJSON();
	        searchParam.pageNo = 1;
	        searchParam.pageSize = -1;
            var sortName = $('#studentTable').bootstrapTable("getOptions", "none").sortName;
            var sortOrder = $('#studentTable').bootstrapTable("getOptions", "none").sortOrder;
            var values = "";
            for(var key in searchParam){
                values = values + key + "=" + searchParam[key] + "&";
            }
            if(sortName != undefined && sortOrder != undefined){
                values = values + "orderBy=" + sortName + " "+sortOrder;
            }

			jp.downloadFile('${ctx}/buzz/student/student/export?'+values);
	  })

	  $("#search").click("click", function() {// 绑定查询按扭
		  $('#studentTable').bootstrapTable('refresh');
		});

	 $("#reset").click("click", function() {// 绑定查询按扭
		  $("#searchForm  input").val("");
		  $("#searchForm  select").val("");
		   $("#searchForm  .select-item").html("");
		  $('#studentTable').bootstrapTable('refresh');
		});


	});

  function getIdSelections() {
        return $.map($("#studentTable").bootstrapTable('getSelections'), function (row) {
            return row.id
        });
    }

  function deleteAll(){

		jp.confirm('确认要删除该学生登记	记录吗？', function(){
			jp.loading();
			jp.get("${ctx}/buzz/student/student/deleteAll?ids=" + getIdSelections(), function(data){
         	  		if(data.success){
         	  			$('#studentTable').bootstrapTable('refresh');
         	  			jp.success(data.msg);
         	  		}else{
         	  			jp.error(data.msg);
         	  		}
         	  	})

		})
  }

     //刷新列表
  function refresh(){
  	$('#studentTable').bootstrapTable('refresh');
  }
  function add(){
		jp.go("${ctx}/buzz/student/student/form/add");
	}

  function edit(id){
	  if(id == undefined){
		  id = getIdSelections();
	  }
	  jp.go("${ctx}/buzz/student/student/form/edit?id=" + id);
  }

 function view(id){//没有权限时，不显示确定按钮
      if(id == undefined){
             id = getIdSelections();
      }
         jp.go("${ctx}/buzz/student/student/form/view?id=" + id);
 }





  function detailFormatter(index, row) {
	  var htmltpl =  $("#studentChildrenTpl").html().replace(/(\/\/\<!\-\-)|(\/\/\-\->)/g,"");
	  var html = Mustache.render(htmltpl, {
			idx:row.id
		});
	  $.get("${ctx}/buzz/student/student/detail?id="+row.id, function(student){
    	var studentChild1RowIdx = 0, studentChild1Tpl = $("#studentChild1Tpl").html().replace(/(\/\/\<!\-\-)|(\/\/\-\->)/g,"");
		var data1 =  student.studententryList;
		for (var i=0; i<data1.length; i++){
			data1[i].dict = {};
			addRow('#studentChild-'+row.id+'-1-List', studentChild1RowIdx, studentChild1Tpl, data1[i]);
			studentChild1RowIdx = studentChild1RowIdx + 1;
		}


      })

        return html;
    }

	function addRow(list, idx, tpl, row){
		$(list).append(Mustache.render(tpl, {
			idx: idx, delBtn: true, row: row
		}));
	}

</script>
<script type="text/template" id="studentChildrenTpl">//<!--
	<div class="tabs-container">
		<ul class="nav nav-tabs">
				<li class="active"><a data-toggle="tab" href="#tab-{{idx}}-1" aria-expanded="true">关联家长</a></li>
		</ul>
		<div class="tab-content">
				 <div id="tab-{{idx}}-1" class="tab-pane fade in active">
						<table class="ani table">
						<thead>
							<tr>
								<th>备注信息</th>
								<th>关联家长</th>
							</tr>
						</thead>
						<tbody id="studentChild-{{idx}}-1-List">
						</tbody>
					</table>
				</div>
		</div>//-->
	</script>
	<script type="text/template" id="studentChild1Tpl">//<!--
				<tr>
					<td>
						{{row.remarks}}
					</td>
					<td>
						{{row.family}}
					</td>
				</tr>//-->
	</script>
